import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/base/Refresher.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/space.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/pages/circle/model/CircleModel.dart';
import 'package:huajianshe_app/pages/main/MomentItem.dart';
import 'package:huajianshe_app/pages/main/model/MomentModel.dart';
import 'package:huajianshe_app/pages/user_info/UserMomentHeader.dart';
import 'package:huajianshe_app/pages/user_info/UserMomentItem.dart';
import 'package:huajianshe_app/pages/user_info/model/UserModel.dart';
import 'package:huajianshe_app/widget/BgImageLayout.dart';
import 'UserInfoAppBar.dart';

///某人的所有动态
class UserMomentsPage extends StatefulWidget {

  final int? momentType; //这个页面只会出现5和6
  final UserInfo? userInfo;
  final CircleInfo? circleInfo;

  const UserMomentsPage({
    Key? key, 
    this.momentType = MomentType.MOMENT_TYPE_ALL_FROM_CAPTION, 
    required this.userInfo,
    this.circleInfo
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _UserMomentsPageState();
  }
}

class _UserMomentsPageState extends BaseState<UserMomentsPage> {

  late SystemUiOverlayStyle overlayStyle;
  late double appbarOpacity;
  late ScrollController controller;
  late int offset = 0;

  @override
  void initState() {
    super.initState();
    overlayStyle = SystemUiOverlayStyle.light;
    appbarOpacity = 0;
    controller = ScrollController();
    controller.addListener(() => {_onScroll()});
    refresher = Refresher<MomentMonthData>(this);
    refresher?.enablePullUp = false;
    refresher?.enablePullDown = false;
    load();
  }

  load() {
    var data = {
      'member_sn': widget.userInfo?.memberSn,
      'get_type':  widget.momentType,//widget.momentType,
      'group_by_month': 1 //是否按月分组 1.是 （默认否）,如果按月分组，没有分页，分页参数无效
    };
    sendReq(Api.momentList(), MomentMonthListResp(), data: data);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is MomentMonthListResp) {
      print('MomentMonthListResp: $t');
      refresher?.load(t.data);
      setState(() => {});
    }
  }

  void _onScroll() {
    // print('position ${controller.position} offset ${controller.offset}');
    int newOffset = controller.offset.toInt() - 50;
    if (newOffset > 50) newOffset = 50;
    if (newOffset < 0) newOffset = 0;
    if (offset == newOffset) return;
    offset = newOffset;
    this.appbarOpacity = offset.toDouble() * 2 * 0.01;
    print('appbarOpacity ${appbarOpacity} offset ${offset}');
    if (this.appbarOpacity > 0.5 && this.overlayStyle != SystemUiOverlayStyle.dark) {
      this.overlayStyle = SystemUiOverlayStyle.dark;
    }
    if (this.appbarOpacity <= 0.5 && this.overlayStyle != SystemUiOverlayStyle.light) {
      this.overlayStyle = SystemUiOverlayStyle.light;
    }
    setState(() { });
  }

  @override
  Widget build(BuildContext context) {
    return
      BgImageLayout(imageHeight: 175, imageUrl: 'bg_user_info', bgColor: Colors.white, overlayStyle: overlayStyle,
          children: [
            refresher?.build(buildChild()) ?? H(0),
            //app bar1
            Container( width: double.infinity,
                child: SafeArea(child: Container(height: 48,
                    child: Row(mainAxisSize: MainAxisSize.max, children: [
                      W(15),
                      LoadImage('ic_white_arrow')
                    ])
                )),
                decoration: BoxDecoration(color: Colors.transparent,
                  boxShadow: [
                    BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.07), offset: Offset(0, -2.5), blurRadius: 14)
                  ]
                ),
            ),
            //app bar2
            Opacity(opacity: appbarOpacity, child: Container(color: Color.fromRGBO(255, 255, 255, 1), width: double.infinity,
                child: SafeArea(child: UserInfoAppBar(userInfo: widget.userInfo,))
            ))
          ]
      );
  }

  Widget? buildChild() {
    // Widget? child = buildEmptyPage();
    // if (child != null) {
    //   return child;
    // }
    print('${refresher?.data?.length}');
    return ListView.separated(
        controller: this.controller,
        itemBuilder: (_, index) {
          if (index == 0) {
            int total = refresher?.data?[0].total;
            return UserMomentHeader(userInfo: widget.userInfo, total: total);
          }
          return UserMomentItem(refresher?.data?[index - 1], widget.circleInfo, hideTopLine: index == 1,);
        },
        separatorBuilder: (_, index) => H(0),
        itemCount: ((refresher?.data?.length ?? 0) + 1)
    );
  }
}